/**
 *    Copyright (C) 2009 Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CONTAINERS_STREAM_POSIX_CONTAINER_HPP
#define _CUBEFS_CONTAINERS_STREAM_POSIX_CONTAINER_HPP

#include <cubefs/api/api.hpp>
#include <cubefs/core/container/container.hpp>

#include "names.hpp"
#include "manager.hpp"

#define CUBEFS_SOURCE "cubefs/containers/stream/posix/container.hpp:" CUBEFS_AT

namespace cubefs {namespace ctr {

CUBEFS_CONTAINER_PART_NO_CTR_BEGIN(cubefs::ctr::stream::PageManagerPartName)

	typedef typename Base::Profile												Profile;
    typedef typename Base::PageManagerType                       				PageManagerType;

    typedef typename PageManagerType::Page                                      Page;
    typedef typename PageManagerType::ID                                        ID;
    typedef typename TypeLocator<Profile, Root>::Type            				RootMap;

    typedef typename Base::Types::PageDispatcher                                PageDispatcher;

    static const int PAGE_SIZE                                                  = PageManagerType::PAGE_SIZE;

private:
    RootMap*            roots_;
    StreamPageManager<
		Profile,
		RootMap,
		PageDispatcher
	>					manager;

public:

    ContainerPart(MyType &me): Base(me), me_(me), manager(me.reflection())
    {
        roots_      = manager.roots();
    }

    virtual ~ContainerPart() throw () {}

    RootMap* roots1() {
        return roots_;
    }

    virtual TreeMap *GetRootMap() {
        return roots_;
    }

    virtual PageManagerType* mgr() {
        return &manager;
    }

    virtual Manager* GetManager() {
        return &manager;
    }

    virtual void SetManager(Manager* manager) {
    	throw CubefsException(CUBEFS_SOURCE, "This container doesn't support manual PageManager setting");
    }

    virtual cubefs::api::Model* GetCollectionByName(BigInt name) {

        ID id;

        if (roots_->GetValue(name, 0, id))
        {
            TypedIDValueImpl idValue(&id);
            return me_.GetCollection(&idValue);
        }
        else {
            return NULL;
        }
    }

CUBEFS_CONTAINER_PART_END

}}



#undef CUBEFS_SOURCE
#endif
